package com.coolgor.coldot.dao.auth;

import java.util.List;

import com.coolgor.coldot.entity.auth.LocalUser;
import org.apache.ibatis.annotations.*;


@Mapper
public interface LocalUserDao {

	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_auth_localuser
			WHERE
				user_id = #{userID}
		</script>		
	""")
	@Results(id = "LocalUserMap", value = {
		@Result(property = "userID", column = "user_id"),
		@Result(property = "username", column = "username"),
		@Result(property = "password", column = "password"),
		@Result(property = "nameCN", column = "name_cn"),
		@Result(property = "status", column = "status"),
		@Result(property = "createTime", column = "create_time"),
		@Result(property = "lastEditTime", column = "last_edit_time"),
		@Result(property = "citizenCard", column = "citizen_card"),
		@Result(property = "citizenId", column = "citizen_id"),
		@Result(property = "gender", column = "gender"),
		@Result(property = "birthday", column = "birthday"),
		@Result(property = "address", column = "address"),
		@Result(property = "mobilephone", column = "mobilephone"),
		@Result(property = "telephone", column = "telephone"),
		@Result(property = "email", column = "email"),
		@Result(property = "wechat", column = "wechat"),
		@Result(property = "QQ", column = "qq"),
		@Result(property = "homepage", column = "homepage"),
		@Result(property = "remark", column = "remark")
	})
	LocalUser queryLocalUserById(Long userID);


	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_auth_localuser
			WHERE
				username = #{username}
		</script>		
	""")
	@ResultMap("LocalUserMap")
	LocalUser queryLocalUserByUsername(String username);



	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_auth_localuser
			<where>
				<if test="localUserCondition.userID != null">
					and user_id = #{localUserCondition.userID}
				</if>
				<if test="localUserCondition.username != null">
					and username like '${localUserCondition.username}'
				</if>
				<if test="localUserCondition.status != null">
					and status = #{localUserCondition.status}
				</if>
				<if test="localUserCondition.nameCN != null">
					and name_cn like '${localUserCondition.nameCN}'
				</if>
			</where>
			LIMIT #{rowIndex},#{pageSize};		
		</script>		
	""")
	@ResultMap("LocalUserMap")
	List<LocalUser> queryLocalUserList(@Param("localUserCondition") LocalUser localUserCondition,
										@Param("rowIndex") int rowIndex,
										@Param("pageSize") int pageSize);


	@Select("""
		<script>
			SELECT
				count(1)
			FROM
				tb_coldot_auth_localuser
			<where>
				<if test="localUserCondition.userID != null">
					and user_id = #{localUserCondition.userID}
				</if>
				<if test="localUserCondition.username != null">
					and username like '${localUserCondition.username}'
				</if>
				<if test="localUserCondition.status != null">
					and status = #{localUserCondition.status}
				</if>
				<if test="localUserCondition.nameCN != null">
					and name_cn like '${localUserCondition.nameCN}'
				</if>
			</where>		
		</script>		
	""")
	int queryLocalUserCount(@Param("localUserCondition") LocalUser localUserCondition);


	@Insert("""
		<script>
			INSERT INTO tb_coldot_auth_localuser
				(username,password,name_cn,status,create_time,last_edit_time,citizen_card,citizen_id,
				gender,birthday,address,mobilephone,telephone,email,wechat,qq,homepage,remark)
			VALUES
				(#{username},#{password},#{nameCN},#{status},#{createTime},#{lastEditTime},
				#{citizenCard},#{citizenId},#{gender},#{birthday},#{address},#{mobilephone},#{telephone},
				#{email},#{wechat},#{QQ},#{homepage},#{remark})		
		</script>		
	""")
	@Options(useGeneratedKeys = true, keyColumn = "user_id", keyProperty = "userID")
	int insertLocalUser(LocalUser localUser);


	@Update("""
		<script>
			UPDATE
				tb_coldot_auth_localuser
			<set>
				<if test="username != null">username=#{username},</if>
				<if test="password != null">password=#{password},</if>
				<if test="nameCN != null">name_cn=#{nameCN},</if>
				<if test="status != null">status=#{status},</if>
				<if test="createTime != null">create_time=#{createTime},</if>
				<if test="lastEditTime != null">last_edit_time=#{lastEditTime},</if>
				<if test="citizenCard != null">citizen_card=#{citizenCard},</if>
				<if test="citizenId != null">citizen_id=#{citizenId},</if>
				<if test="gender != null">gender=#{gender},</if>
				<if test="birthday != null">birthday=#{birthday},</if>
				<if test="address != null">address=#{address},</if>
				<if test="mobilephone != null">mobilephone=#{mobilephone},</if>
				<if test="telephone != null">telephone=#{telephone},</if>
				<if test="email != null">email=#{email},</if>
				<if test="wechat != null">wechat=#{wechat},</if>
				<if test="QQ != null">qq=#{QQ},</if>
				<if test="homepage != null">homepage=#{homepage},</if>
				<if test="remark != null">remark=#{remark}</if>
			</set>
			WHERE
				user_id = #{userID}		
		</script>		
	""")
	int updateLocalUser(LocalUser localUser);


	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_auth_localuser
			WHERE
				user_id = #{userID}		
		</script>		
	""")
	int deleteLocalUser(LocalUser localUser);


	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_auth_localuser
			WHERE user_id IN
			<foreach collection="list" item="userID" open="(" separator="," close=")">
				#{userID}
			</foreach>		
		</script>		
	""")
	int batchDeleteLocalUser(List<Long> userIDList);

}
